package com.codingfuture.web.controller;

import com.codingfuture.entity.Question;
import com.codingfuture.service.QuestionService;
import com.codingfuture.vo.Result;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @GetMapping("/questions")
    public Result list() {
        List<Question> list = questionService.list();
        return Result.ok(list);
    }

    @PostMapping("/question")
    public Result add(Question question) {
        int i = questionService.add(question);
        System.out.println(question);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.err();
        }
    }

    @DeleteMapping("/question/{id}")
    public Result deleteById(@PathVariable Integer id){
        int i = questionService.deleteById(id);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.err();
        }
    }
    @PutMapping("/question/{id}")
    public Result updateById(@PathVariable Integer id, @RequestBody Question question){
        question.setQid(id);
        int i = questionService.updateById(question);
        if (i > 0) {
            return Result.ok();
        } else {
            return Result.err();
        }


    }
}
